package vn.sunnet.game.SinhTon.screen;

import java.util.ArrayList;

import vn.sunnet.game.SinhTon.global.Assets;
import vn.sunnet.game.SinhTon.global.OverlapTester;
import vn.sunnet.game.SinhTon.global.Setting;
import vn.sunnet.game.SinhTon.object.Blockhouse;
import vn.sunnet.game.SinhTon.object.GunShopObject;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ButtonGroup;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class ShopGun extends BaseScreen {

	private Button btnBack, btnTiepTuc, btnSuDung, btnMua, btnNapXu, btnLocot,
			btnGun;

	private int select_Gun = 2;
	private int select_Locot = 2;
	private float x_TouchDown, y_TouchDown;
	private ArrayList<GunShopObject> arrShopGun; // doi tuong sung
	private ArrayList<Blockhouse> arrBlockhouse; // doi tuong locot

	public boolean isNotEnoughCoin; // Kiem tra xem co du xu de mua sung hay ko
	private boolean isLocot; // Mac dinh la false vi khi vao shop thi tab
								// sung mac dinh hien len truoc
	// Dung de cho 2 nut sung va locot luon o 2 trang thai khac nhau
	private ButtonGroup buttonGroup;

	// Dung de ve ra cac hinh vuong bao doi tuong(dung de test)
	// private ShapeRenderer renderer;
	public ShopGun(MyGame game) {
		super(game);
		if (!Assets.GET_SHOP) {
			Assets.getShop();
			Assets.font1.setScale(0.6f);
		}

		buttonGroup = new ButtonGroup();
		buttonGroup.setMaxCheckCount(1);

		initButton();
		// Khoi tao mang chua cac sung va locot co id tu 2 den 6
		// vi mac dinh id = 1 la free
		arrShopGun = new ArrayList<GunShopObject>();
		for (int i = 0; i < 5; i++) {
			arrShopGun.add(new GunShopObject(i + 2));
		}

		arrBlockhouse = new ArrayList<Blockhouse>();
		for (int i = 0; i < 5; i++) {
			arrBlockhouse.add(new Blockhouse(i + 2));
		}
		// renderer = new ShapeRenderer();
	}

	// private void renderDebug() {
	// renderer.setProjectionMatrix(camera.combined);
	// renderer.begin(ShapeType.Rectangle);
	//
	// if (isLocot)
	//
	// for (Blockhouse bk : arrBlockhouse) {
	// // renderer.setColor(Color.RED);
	// renderer.rect(bk.bounds.x, bk.bounds.y, bk.bounds.width,
	// bk.bounds.height);
	// }
	// else
	// for (GunShopObject gs : arrShopGun) {
	// // renderer.setColor(Color.RED);
	// renderer.rect(gs.bounds.x, gs.bounds.y, gs.bounds.width,
	// gs.bounds.height);
	// }
	// renderer.end();
	// }

	@Override
	public void render(float delta) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

		camera.update();
		stage.act(delta);

		if (isLocot)
			updateSelectOptionLocot(delta);
		else
			updateSelectOptionGun(delta);
		batch.begin();

		batch.draw(Assets.ttBgShopGun, 0, 0);

		Assets.font.drawWrapped(batch, String.valueOf(Assets.coin), 196, 455,
				210, HAlignment.CENTER);

		if (isLocot) {
			drawPositionLocot();
			drawInfoLocot();
		} else {
			drawPositionGun();
			drawInfoGun();
		}
		batch.end();
		// Neu ma dang o tab mua Locot thi remove nut su dung còn nguoc lai thi
		// addActor
		if (isLocot)
			btnSuDung.remove();
		else
			stage.addActor(btnSuDung);
		stage.draw();

		batch.begin();
		// Ve thong bao khi khong du so xu de mua sung
//		if (isNotEnoughCoin)
//			batch.draw(Assets.ttNotEnoughCoin, 206.5f, 139.5f, 387, 201);

		batch.end();
		// renderDebug();
	}

	private void drawPositionGun() {
		for (int i = 0; i < arrShopGun.size(); i++) {
			if (select_Gun == arrShopGun.get(i).getId())
				batch.draw(arrShopGun.get(i).getTexture_hover(),
						arrShopGun.get(i).bounds.x, arrShopGun.get(i).bounds.y,
						arrShopGun.get(i).bounds.width,
						arrShopGun.get(i).bounds.height);
			else
				batch.draw(arrShopGun.get(i).getTexture(),
						arrShopGun.get(i).bounds.x, arrShopGun.get(i).bounds.y,
						arrShopGun.get(i).bounds.width,
						arrShopGun.get(i).bounds.height);
		}

	}

	private void drawInfoGun() {
		for (int i = 0; i < arrShopGun.size(); i++) {
			if (select_Gun == arrShopGun.get(i).getId()) {
				batch.draw(arrShopGun.get(i).getTextureSung(), 169, 246, 150,
						150);
				// Ve so xu de mua sung
				Assets.font.drawWrapped(batch,
						String.valueOf(arrShopGun.get(i).getCoinValue()), 189,
						249, 130, HAlignment.CENTER);
				// Ve cac thong tin ve sung
				Assets.font1.draw(batch, "AD ", 565, 341);
				Assets.font1.draw(batch, "AOE", 565, 313);
				Assets.font1.draw(batch, "Reload ", 565, 286);

				batch.draw(Assets.ttBar, 650, 323, 115 * arrShopGun.get(i)
						.getDamage() / 25, 9);
				batch.draw(Assets.ttBar, 650, 296, 115 * arrShopGun.get(i)
						.getDamageRegion() / 150, 9);
				batch.draw(Assets.ttBar, 650, 268, 115 * arrShopGun.get(i)
						.getTimeReload() / 4, 9);

				if (!arrShopGun.get(select_Gun - 2).isCanBuy()) {
					Assets.font.setScale(0.83f);
					Assets.font.draw(batch, "Bạn chỉ được mua khi vượt qua map "
							+ select_Gun, 285, 174);
					Assets.font.setScale(1f);
				}

			}
		}
		// Neu sung da duoc mua roi thi ve anh nay
		if (Setting.getGun(select_Gun) && select_Gun > 1)
			batch.draw(Assets.ttBought, 18, 330, 120, 60);
	}

	private void drawPositionLocot() {
		for (int i = 0; i < arrBlockhouse.size(); i++) {
			if (select_Locot == arrBlockhouse.get(i).getId())
				batch.draw(arrBlockhouse.get(i).getTexture_hover(),
						arrBlockhouse.get(i).bounds.x,
						arrBlockhouse.get(i).bounds.y,
						arrBlockhouse.get(i).bounds.width,
						arrBlockhouse.get(i).bounds.height);
			else
				batch.draw(arrBlockhouse.get(i).getTexture(),
						arrBlockhouse.get(i).bounds.x,
						arrBlockhouse.get(i).bounds.y,
						arrBlockhouse.get(i).bounds.width,
						arrBlockhouse.get(i).bounds.height);
		}
		// Neu locot da duoc mua roi thi ve anh nay
		if (Setting.getLocot(select_Locot))
			batch.draw(Assets.ttBought, 18, 330, 120, 60);

	}

	// Ve thong tin ve locot va gun
	private void drawInfoLocot() {
		for (int i = 0; i < arrBlockhouse.size(); i++) {
			if (select_Locot == arrBlockhouse.get(i).getId()) {
				batch.draw(Assets.trLocot.get(select_Locot - 1), 122, 276, 234,
						108);
				// Ve so xu de mua sung
				Assets.font.drawWrapped(batch,
						String.valueOf(arrBlockhouse.get(i).getCoinValue()),
						189, 249, 130, HAlignment.CENTER);
				// Ve cac thong tin ve sung
				Assets.font1.draw(batch, "Tên ", 565, 341);
				Assets.font1.draw(batch, "Máu", 565, 313);
				Assets.font1.draw(batch, "Giá ", 565, 286);

				Assets.font1.draw(batch, arrBlockhouse.get(select_Locot - 2)
						.getName(), 649, 342);

				batch.draw(Assets.ttBar, 650, 296, 115 * arrBlockhouse.get(i)
						.getBlood() / 225, 9);
				batch.draw(Assets.ttBar, 650, 268, 115 * arrBlockhouse.get(i)
						.getCoinValue() / 3800, 9);

				if (!arrBlockhouse.get(select_Locot - 2).isCanBuy()) {
					Assets.font.setScale(0.83f);
					Assets.font.draw(batch, "Bạn chỉ được mua khi vượt qua map "
							+ arrBlockhouse.get(select_Locot - 2)
									.getMapCanBuy(), 285, 174);
					Assets.font.setScale(1f);
				}
			}
		}
	}

	// Cap nhat khi lua chon locot
	private void updateSelectOptionLocot(float time) {
		for (Blockhouse bk : arrBlockhouse) {
			if (OverlapTester.pointInRectangle(bk.bounds, x_TouchDown,
					y_TouchDown)) {
				select_Locot = bk.getId();
			}

		}
	}

	// Cap nhat khi chon sung
	private void updateSelectOptionGun(float time) {
		for (GunShopObject gs : arrShopGun) {
			if (OverlapTester.pointInRectangle(gs.bounds, x_TouchDown,
					y_TouchDown)) {
				select_Gun = gs.getId();
			}
		}
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		super.resize(width, height);
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		super.resume();
	}

	@Override
	public void dispose() {
		super.dispose();
		if (Assets.mOptionShop != null)
			Assets.mOptionShop.dispose();
	}

	@Override
	public boolean keyDown(int keycode) {
		if (keycode == Keys.BACK)
			game.setScreen(new StageScreen(game));
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		x_TouchDown = screenX * Assets.SCREEN_WIDTH / Gdx.graphics.getWidth();
		y_TouchDown = (Gdx.graphics.getHeight() - screenY)
				* Assets.SCREEN_HEIGHT / Gdx.graphics.getHeight();
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		isNotEnoughCoin = false;
		return false;
	}

	private void initButton() {
		btnBack = new Button(Assets.skin, "btnBack");
		btnBack.setBounds(12, 410, 77, 49);

		btnBack.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);

				game.setScreen(new StageScreen(game));
			}
		});

		btnTiepTuc = new Button(Assets.skin, "btnTiepTuc");
		btnTiepTuc.setBounds(607, 410, 173, 56);

		btnTiepTuc.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);

				game.setScreen(new ShopSkill(game));
			}
		});

		btnMua = new Button(Assets.skin, "btnMua");
		btnMua.setBounds(16, 215, 131, 44);

		btnMua.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				// Mua sung
				if (isLocot) {
					// Neu ma chua mua locot nay thi
					if (!Setting.getLocot(select_Locot)) {
						if (arrBlockhouse.get(select_Locot - 2).isCanBuy()) {
							if (Assets.coin >= arrBlockhouse.get(
									select_Locot - 2).getCoinValue()) {
								if (Setting.getBoolean(Setting.SOUND))
									Assets.arrSoundInShop.get(0).play(1);
								isNotEnoughCoin = false;
								Assets.coin -= arrBlockhouse.get(
										select_Locot - 2).getCoinValue();
								Setting.setCoin(Assets.coin);
								Setting.setLocot(select_Locot);
							
								game.request.trackBuyItem(
										arrBlockhouse.get(select_Locot - 2)
												.getCoinValue(),
										4 + select_Locot, Assets.map);
							} else {
								isNotEnoughCoin = true;
								game.dialog.notEnoughtCoin();
							}
						}
					}
				} else {
					// Neu ma sung chua duoc mua
					if (!Setting.getGun(select_Gun)) {
						if (arrShopGun.get(select_Gun - 2).isCanBuy()) {
							if (Assets.coin >= arrShopGun.get(select_Gun - 2)
									.getCoinValue()) {
								if (Setting.getBoolean(Setting.SOUND))
									Assets.arrSoundInShop.get(2).play(1);
								isNotEnoughCoin = false;
								Assets.coin -= arrShopGun.get(select_Gun - 2)
										.getCoinValue();
								Setting.setCoin(Assets.coin);
								// Setting.putBoolean(select_Gun, true);

								Setting.setGun(select_Gun);
								game.request.trackBuyItem(
										arrShopGun.get(select_Gun - 2)
												.getCoinValue(),
										select_Gun - 1, Assets.map);
								game.dialog.showToast("Bấm vào nút Sử Dụng để có thể dùng súng này khi chơi!");
								
							} else {
								isNotEnoughCoin = true;
								game.dialog.notEnoughtCoin();
							}
						}
					}
				}
			}
		});
		btnNapXu = new Button(Assets.skin, "btnNapXu");
		btnNapXu.setBounds(668, 196, 109, 44);

		btnNapXu.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.arrSoundInShop.get(3).play(1);
				game.dialog.confirmBuyCoins();
				Assets.coin = Setting.getCoin();
			}
		});
		btnSuDung = new Button(Assets.skin, "btnSuDung");
		btnSuDung.setBounds(335, 215, 131, 44);

		btnSuDung.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getGun(select_Gun)) {
					Assets.arrSoundInShop.get(4).play(1);
					int size = Setting.arrGunsCanUse.size;
					if (Setting.arrGunsCanUse.get(size - 1) != select_Gun)
						Setting.arrGunsCanUse.add(select_Gun);
					for (int i = 0; i < Setting.arrGunsCanUse.size; i++) {
						Gdx.app.log(getClass().toString(), " "
								+ Setting.arrGunsCanUse.get(i));
					}
				} else {
					Gdx.app.log(getClass().toString(), "Cưng chưa mua súng "
							+ select_Gun);
				}
			}
		});
		stage.addActor(btnBack);
		stage.addActor(btnTiepTuc);
		stage.addActor(btnMua);
		stage.addActor(btnNapXu);

		// btnLocot = new Button(Assets.skin, "btnLocot");
		btnLocot = new Button(Assets.skin.getDrawable("locot"),
				Assets.skin.getDrawable("locot_hover"),
				Assets.skin.getDrawable("locot_hover"));
		btnLocot.setBounds(134, 134, 131, 44);
		// btnLocot.setChecked(!isLocot);
		btnLocot.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				isLocot = true;
			}
		});

		// btnGun = new Button(Assets.skin, "btnGun");
		btnGun = new Button(Assets.skin.getDrawable("gun"),
				Assets.skin.getDrawable("gun_hover"),
				Assets.skin.getDrawable("gun_hover"));
		btnGun.setBounds(0, 134, 131, 44);
		// btnGun.setChecked(isLocot);
		btnGun.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				isLocot = false;
			}
		});

		buttonGroup.add(btnLocot);
		buttonGroup.add(btnGun);

		stage.addActor(btnLocot);
		stage.addActor(btnGun);
	}
}
